package com.tgy.leetcode;

import java.util.Stack;

/**
 * @Author: tgy
 * @Date: 12/3/20 11:22 AM
 *
 * https://leetcode-cn.com/problems/daily-temperatures/
 */
public class _739_每日温度 {

    public int[] dailyTemperatures(int[] T) {

        if (T == null || T.length == 0) {

            return new int[0];
        }

        // 单调递减
        Stack<Integer> stack = new Stack<>();

        int[] ret = new int[T.length];

        for (int i = 0; i < T.length; i++) {


            if (stack.isEmpty()) {

                stack.push(i);
                continue;
            }

            while (!stack.isEmpty() && T[stack.peek()] <= T[i]) {

                int index = stack.pop();
                ret[index] = i - index;
            }

            stack.push(i);
        }


        return ret;
    }
}
